package com.ruoyi.web.controller.admin;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.ruoyi.system.domain.PetAdoption;
import com.ruoyi.system.service.IPetAdoptionService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.PetInfo;
import com.ruoyi.system.service.IPetInfoService;

import static java.util.stream.Collectors.toMap;

/**
 * 宠物信息Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/pet")
public class PetController extends BaseController
{
    @Autowired
    private IPetInfoService petInfoService;

    @Autowired
    private IPetAdoptionService petAdoptionService;

    /**
     * 查询宠物信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(PetInfo petInfo)
    {
        startPage();
        List<PetInfo> list = petInfoService.selectPetInfoList(petInfo);
        if(CollectionUtils.isNotEmpty(list)){
            List<Long> petIds = list.stream().map(PetInfo::getPetId).distinct().collect(Collectors.toList());
            List<PetAdoption> petAdoptions = petAdoptionService.selectPetAdoptionByPetIds(petIds);
            if(CollectionUtils.isNotEmpty(petAdoptions)){
                Map<Long, PetAdoption> petAdoptionMap = petAdoptions.stream().collect(toMap(PetAdoption::getPetId, Function.identity(),(k1, k2) -> k2));
                list = list.stream().peek(item -> {
                    PetAdoption petAdoption = petAdoptionMap.get(item.getPetId());
                    if(ObjectUtils.isNotEmpty(petAdoption) && petAdoption.getStatus().equals("0")){
                        item.setStatus("2"); //领养审核中
                    }
                }).collect(Collectors.toList());
            }
        }
        return getDataTable(list);
    }

    /**
     * 获取宠物信息详细信息
     */
    @GetMapping(value = "/get/{petId}")
    public AjaxResult getInfo(@PathVariable("petId") Long petId)
    {
        return success(petInfoService.selectPetInfoById(petId));
    }

    /**
     * 新增宠物信息
     */
    @Log(title = "宠物信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody PetInfo petInfo)
    {
        return toAjax(petInfoService.insertPetInfo(petInfo));
    }

    /**
     * 修改宠物信息
     */
    @Log(title = "宠物信息", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult edit(@RequestBody PetInfo petInfo)
    {
        return toAjax(petInfoService.updatePetInfo(petInfo));
    }

    /**
     * 删除宠物信息
     */
    @Log(title = "宠物信息", businessType = BusinessType.DELETE)
    @PostMapping("/del/{petIds}")
    public AjaxResult remove(@PathVariable Long[] petIds)
    {
        return toAjax(petInfoService.deletePetInfoByIds(petIds));
    }
}
